package stdlib.util

import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import java.util.Timer
import java.util.TimerTask


fun testTimer() = runBlocking {
    val timer = Timer()
    println("create timer")
    timer.schedule(object : TimerTask() {
        override fun run() {
            println("timer run")
            timer.cancel()
        }
    }, 3000)
}

fun testTimer3() = runBlocking {
    val timer = Timer()
    println("create timer")
    var count = 0
    timer.schedule(object : TimerTask() {
        override fun run() {
            println("count: $count")
            count += 1
            if (count > 3) {
                this.cancel()
                timer.cancel()
                println("cancel")
            }
        }
    }, 1000, 3000)
    println("timer over")
}

fun testTimer2() = CoroutineScope(Dispatchers.Default).launch {
    delay(2000)
    println("testTimer2 over")
}

fun main() {
//    testTimer2()
//    testTimer()
    testTimer3()
    println("main over")
}